package com.zmn.plat.admin.controller.part.category.second;

import java.util.ArrayList;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartCategory;

/**
 * 类描述：
 *
 * @author DaiZhichao
 * @since 2018/10/16 11:23
 */
@RestController
@RequestMapping("/part/category/second/ajax")
public class PartCategorySecondModifyController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartCategorySecondModifyController.class);

    @RequestMapping(value = "/updateStatus", produces = "application/json; charset=utf-8")
    @RequiresPermissions("/part/category/second/ajax/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "ids[]") ArrayList<Integer> ids, @RequestParam(value = "status") int status) {
        if (ids.isEmpty()) {
            return ResponseDTO.fail("请选择要更改状态的配件！");
        }
        partCategoryService.updateStatusBatch(ids, status, getMcStaffName(request));
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "/addEdit", produces = "application/json; charset=utf-8")
    @RequiresPermissions("/part/category/second/ajax/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody(required = false) PartCategory partCategory) {

        if (partCategoryBService.findIdenticalCategory(partCategory)) {
            return ResponseDTO.fail("分类名称已存在，请勿重复添加");
        }

        String lable = null;
        try {
            if (Objects.isNull(partCategory.getCategId())) {
                lable = "新增配件分类";
                partCategoryService.insert(partCategory, getMcStaff());
            } else {
                lable = "修改配件分类";
                partCategoryService.updateByKey(partCategory, getMcStaff());
            }
        } catch (Exception e) {
            logger.error(String.format("%s失败！", lable), e);
            return ResponseDTO.fail(String.format("%s失败！", lable));
        }
        return ResponseDTO.success(String.format("%s成功！", lable));
    }

    @RequestMapping(value = "/editname", produces = "application/json; charset=utf-8")
    @RequiresPermissions("/part/category/second/ajax/addEdit.action")
    public ResponseDTO editName(HttpServletRequest request, Integer categId, String name) {

        PartCategory partCategory = partCategoryService.findByKey(categId);
        partCategory.setCategId(categId);
        partCategory.setName(name);
        partCategory.setUpdater(getMcStaffName(request));

        partCategoryService.updateByKey(partCategory, getMcStaff());

        return ResponseDTO.success(String.format("修改成功！"));
    }

    @RequestMapping("/disable")
    @RequiresPermissions("/part/category/second/ajax/updateStatus.action")
    public ResponseDTO partCategoryDisable(HttpServletRequest request, int categId) {

        if (categId == 0) {
            return ResponseDTO.fail("修改状态失败，CategId不能为空！");
        }

        Integer flag = partCategoryService.updateStatus(categId, (short)1, getMcStaffName(request));

        ResponseDTO resultDTO;
        if (flag > 0) {
            resultDTO = ResponseDTO.success(true,"修改状态成功！");
        } else {
            resultDTO = ResponseDTO.fail("修改状态失败！");
        }
        return resultDTO;
    }

    @RequestMapping("/enable")
    @RequiresPermissions("/part/category/second/ajax/updateStatus.action")
    public ResponseDTO partCategoryEisable(HttpServletRequest request, int categId) {

        if (categId == 0) {
            return ResponseDTO.fail("修改状态失败，CategId不能为空！");
        }

        Integer flag = partCategoryService.updateStatus(categId, (short)2, getMcStaffName(request));

        ResponseDTO resultDTO;
        if (flag > 0) {
            resultDTO = ResponseDTO.success(true,"修改状态成功！");
        } else {
            resultDTO = ResponseDTO.fail("修改状态失败！");
        }
        return resultDTO;
    }
}
